package com.example.huangds.myapplication;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.widget.ImageView;

public class GoodsInfoActivity extends Activity {
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_info);

        imageView = findViewById(R.id.goods_pics);
        Bitmap first = BitmapFactory.decodeResource(getResources(), R.mipmap.kyuncoupon);
        Bitmap second = BitmapFactory.decodeResource(getResources(), R.mipmap.goods);
        Bitmap result = mergeBitmap(first, second);
        imageView.setImageBitmap(result);
    }

    /**
     * 纵向拼接两张图片
     * @param first
     * @param second
     * @return
     */
    private Bitmap mergeBitmap(Bitmap first, Bitmap second) {
        int width = first.getWidth();
        int height = first.getHeight() + second.getHeight();
        //创建一个空的Bitmap(内存区域),宽度等于第一张图片的宽度，高度等于两张图片高度总和
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //将bitmap放置到绘制区域,并将要拼接的图片绘制到指定内存区域
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(first, 0, 0, null);
        canvas.drawBitmap(second, 0, first.getHeight(), null);
        return bitmap;
    }
}
